package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.BankCityPrInfo;
import com.tiza.xiaoyoubao.entity.bean.JsonBean;
import com.tiza.xiaoyoubao.utils.GetJsonDataUtil;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by mapple.
 * 银行所在城市(省)
 */
public class BankCityListFragment extends BaseFragment {

    private ArrayList<JsonBean> provinceItems = new ArrayList<>();
    private ArrayList<ArrayList<String>> cityItems = new ArrayList<>();
    @BindView(R.id.lv_listView)
    ListView mListView;
    private CityAdapter mCityAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_get_bank_city;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        initJsonData();
        mCityAdapter = new CityAdapter();
        mListView.setAdapter(mCityAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle args=new Bundle();
                args.putStringArrayList("city",cityItems.get(position));
                args.putString("province",provinceItems.get(position).getName());
                UIHelper.showBankCity(mActivity,args);
                finish();
            }
        });
    }

    class CityAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return provinceItems.size();
        }

        @Override
        public Object getItem(int position) {
            return provinceItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_city_info, null);
                holder = new ViewHolder();
                holder.tvCityName = (TextView) convertView.findViewById(R.id.tv_City);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvCityName.setText(provinceItems.get(position).getName());
            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tvCityName;
    }

    private void initJsonData() {//解析数据

        String JsonData = new GetJsonDataUtil().getJson(mActivity, "province.json");//获取assets目录下的json文件数据
        ArrayList<JsonBean> jsonBean = parseData(JsonData);//用Gson 转成实体
        /**
         * 添加省份数据
         *
         * 注意：如果是添加的JavaBean实体，则实体类需要实现 IPickerViewData 接口，
         * PickerView会通过getPickerViewText方法获取字符串显示出来。
         */
        provinceItems = jsonBean;

        for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
                String CityName = jsonBean.get(i).getCityList().get(c).getName();
                CityList.add(CityName);//添加城市
            }
            /**
             * 添加城市数据
             */
            cityItems.add(CityList);
        }

    }

    public ArrayList<JsonBean> parseData(String result) {//Gson 解析
        ArrayList<JsonBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }
}
